package com.wifi.eshipewifi;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wifi.BaseApplication;
import com.wifi.imp.MessageListener;
import com.wifi.imp.MessageSend;
import com.wifi.utils.WifiUtils;
import com.wifi.view.ConnetAdapter;

import java.util.List;

public class NetWorkConActivity extends Activity implements View.OnClickListener,MessageListener {
    ImageButton ib_back;
    LinearLayout ll_background;
    ProgressBar pb_conn;
    TextView tv_include_tittle;
    TextView tv_right_tittle;
    TextView tv_current_wifi;//当前所连接wifi
    TextView tv_background_conn;//重连背景
    BaseApplication application;
    String TAG="NetWorkSetting";
    private PullToRefreshListView pull_friend_treasure;
    private ConnetAdapter adapter;
    boolean isWifiOpen=false;//wifi是否打开
    SharedPreferences sp;
    ImageView iv_main_switch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net_work_conn);


        pb_conn = (ProgressBar) findViewById(R.id.pb_conn);
        ll_background = (LinearLayout) findViewById(R.id.ll_background);
        tv_background_conn = (TextView) findViewById(R.id.tv_background_conn);
        tv_include_tittle = (TextView) findViewById(R.id.tv_include_tittle);
        tv_right_tittle = (TextView) findViewById(R.id.tv_right_tittle);
        tv_current_wifi = (TextView) findViewById(R.id.tv_current_wifi);
        iv_main_switch = (ImageView) findViewById(R.id.iv_main_switch);
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        tv_include_tittle.setText("所有网络");
        ib_back.setOnClickListener(this);
        iv_main_switch.setOnClickListener(this);
        tv_right_tittle.setOnClickListener(this);

        application = (BaseApplication) getApplication();

        sp= getSharedPreferences("package", Activity.MODE_PRIVATE);
        isWifiOpen = sp.getBoolean("isWifiOpen", false);
        if(isWifiOpen){//wifi开启中
            iv_main_switch.setBackgroundResource(R.drawable.open);
            ll_background.setVisibility(View.GONE);
        }else {//wifi关闭中
            iv_main_switch.setBackgroundResource(R.drawable.close);
            ll_background.setVisibility(View.GONE);
        }


        pull_friend_treasure = (PullToRefreshListView) findViewById(R.id.pull_wifi);
        pull_friend_treasure.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        pull_friend_treasure.getLoadingLayoutProxy(false, true).setPullLabel("加载");
        pull_friend_treasure.getLoadingLayoutProxy(false, true).setRefreshingLabel("加载1");
        pull_friend_treasure.getLoadingLayoutProxy(false, true).setReleaseLabel("加载2");


        pull_friend_treasure.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉刷新
                application.sendMsg("$scan_wifi;");
                isRefresh=true;
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //上拉加载更多
            }
        });
    }

    private boolean isRefresh=false;//下拉刷新
    boolean isWifiSetting=true;//是否是wifi操作
    @Override
    protected void onResume() {//每个activity都要加这一句
        super.onResume();
        if(application==null)
            application = (BaseApplication) getApplication();
        application.setMsgListener(this);
        application.sendMsg("$scan_wifi;");

        boolean isWifiOpen = sp.getBoolean("isWifiOpen", false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                this.finish();
                break;
            case R.id.tv_right_tittle:
                Intent intent=new Intent(NetWorkConActivity.this,NetConnCacheListAcitvity.class);
                startActivity(intent);
                break;
            case R.id.iv_main_switch:
                ll_background.setVisibility(View.VISIBLE);
                pb_conn.setVisibility(View.VISIBLE);
                tv_background_conn.setText("正在连接中，请稍等……");
                isWifiSetting=true;
                if(isWifiOpen){//已打开
                    application.sendMsg("$down_wifi;");
                }else {//已关闭
                    application.sendMsg("$up_wifi;");
                }
                break;

        }
    }

    @Override
    public void MessageGetListener(String msg) {
        try {
            if (msg != null) {
                String s =msg;
                if (s.trim().length() > 0) {
                    Log.i(TAG, "mhandler接收到obj=" + s);
                    String[] s1=s.split(";");
                    if(s1[0].indexOf("scan_wifi")!=-1){//首页数据
                        isWifiSetting=false;
                        if(s1.length>1){
                            ll_background.setVisibility(View.GONE);//空背景
                            pb_conn.setVisibility(View.GONE);//加载界面
                            application.makeToast(NetWorkConActivity.this,s,false);
                            adapter = new ConnetAdapter(NetWorkConActivity.this,s1,application);
                            pull_friend_treasure.setAdapter(adapter);
                            //todo 查询当前已连接wifi状态
                            application.sendMsg("$get_wifi;");
                        }else {
                            tv_background_conn.setText("当前环境无网络连接");
                            ll_background.setVisibility(View.VISIBLE);//空背景
                            pb_conn.setVisibility(View.GONE);//加载界面
                        }
                        if(isRefresh){
                            pull_friend_treasure.onRefreshComplete();
                            isRefresh=false;
                        }
                    }else if(s1[1].indexOf("connect_wifi_done")!=-1){//重启wifi
                        application.makeToast(NetWorkConActivity.this,"重新连接wifi",true);
                        ll_background.setVisibility(View.VISIBLE);//空背景
                        pb_conn.setVisibility(View.VISIBLE);//加载界面
                        tv_background_conn.setText("正在自动重连中，请稍等……");
                        //断线重连
                        String ssid = application.getSsid();
                        String password = application.getPassword();
                        WifiUtils wifiUtils=new WifiUtils(NetWorkConActivity.this);
                        wifiUtils.WifiRebootForSetName(NetWorkConActivity.this,application,ssid,password);
                    }else if(s1[0].indexOf("check_key")!=-1){
                        ll_background.setVisibility(View.GONE);//空背景
                        pb_conn.setVisibility(View.GONE);//加载界面
                        if(s1[1].indexOf("key_right")!=-1){
                            application.makeToast(NetWorkConActivity.this,"wifi成功连接",true);
                            application.sendMsg("$scan_wifi;");
//                                adapter.notifyDataSetChanged();
                        }else {
                            adapter.setCheck(null);
                            adapter.notifyDataSetChanged();
                            application.makeToast(NetWorkConActivity.this,"wifi连接失败",true);
                        }
                    } else if(s1[0].indexOf("auto_connect")!=-1){//自动重连
                        if(s1[1].indexOf("can't_auto_connect")!=-1){

                        }else {//自动重连
                            //todo 连接界面效果显示
//                                $auto_connect;starnet2
//                                obj=$auto_connect;E4:D3:32:2F:62:24,starnet2,starnet2010;
//                                    adapter.setCheck(s1[1]);
                            if(isWifiOpen){//wifi开启的时候才进行自动重连
                                ll_background.setVisibility(View.VISIBLE);//空背景
                                pb_conn.setVisibility(View.GONE);
                                tv_background_conn.setText("正在自动重连中……");
                                String ssid = application.getSsid();
                                String password = application.getPassword();
                                WifiUtils wifiUtils=new WifiUtils(NetWorkConActivity.this);
                                wifiUtils.WifiRebootForSetName(NetWorkConActivity.this,application,ssid,password);
                            }

                        }
                    }else if(s1[0].indexOf("get_wifi")!=-1){

                        if(s1[1].indexOf("no")==-1){//当前所连接的wifi
//                            tv_current_wifi.setText("已连接"+s1[1]);
                            tv_current_wifi.setText("已开启自动连接");
                            adapter.setCheck(s1[1]);//选中已连接的wifi
                        }else {//当前没连接wifi，是否自动连接
//                            tv_current_wifi.setText("未连接wifi，请手动连接");
                            tv_current_wifi.setText("未开启自动连接");
                            if(isWifiOpen){//wifi开关打开下，才自动重启
                                application.sendMsg("$auto_connect;");//自动连接
                            }
                        }
                    }else if(s1[0].indexOf("up_wifi")!=-1){
                        isWifiOpen=true;
                        iv_main_switch.setBackgroundResource(R.drawable.open);
                        application.sendMsg("$scan_wifi;");
                        application.makeToast(NetWorkConActivity.this,"WLAN连接正在打开",true);
                    }else if(s1[0].indexOf("down_wifi")!=-1){
                        isWifiOpen=false;
                        iv_main_switch.setBackgroundResource(R.drawable.close);
                        application.sendMsg("$scan_wifi;");

                        String ssid = application.getSsid();
                        String password = application.getPassword();
                        WifiUtils wifiUtils=new WifiUtils(NetWorkConActivity.this);
                        wifiUtils.WifiRebootForSetName(NetWorkConActivity.this,application,ssid,password);
                    }else if(s1[0].indexOf("first_connect")!=-1){
                        application.makeToast(NetWorkConActivity.this,"连接成功,请稍等",true);
                        ll_background.setVisibility(View.GONE);
                        if(isWifiSetting){//wifi操作
                            application.sendMsg("$scan_wifi;");
                        }else {//连接wifi 检查密码
                            application.sendMsg("$check_key;");
                        }
                    }
                }
            }
            Log.i(TAG, "mhandler接收到msg" +msg);
        } catch (Exception ee) {
            Log.i(TAG, "加载过程出现异常");
            ee.printStackTrace();
        }
    }
}
